package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.common.bean.UserInfo;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.IndexUserAdapter;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.Customer;
import com.bnyy.medicalHousekeeper.bean.IndexData;
import com.bnyy.medicalHousekeeper.bean.Shop;
import com.bnyy.medicalHousekeeper.bean.Subordinate;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/8
 **/
public class IndexUserListActivity extends BaseActivityImpl {
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_filter)
    TextView tvFilter;

    private IndexData.Data.Type type;
    private int page = 1;
    private int size = 20;
    private IndexUserAdapter adapter;

    public static void show(Context context, IndexData.Data.Type type) {
        Intent intent = new Intent(context, IndexUserListActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_index_user_list;
    }

    @Override
    public String getTitleStr() {
        return null;
    }

    @Override
    public boolean unuseRefreshLayout() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        type = (IndexData.Data.Type) getIntent().getSerializableExtra("type");

        if (type.getType() == 4 || type.getType() == 7) {
            tvFilter.setTag(0);
            tvFilter.setVisibility(View.VISIBLE);
            tvFilter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    new XPopup.Builder(mContext)
                            .asBottomList("排序", new String[]{"时间", "健康异常"}, new OnSelectListener() {
                                @Override
                                public void onSelect(int position, String text) {
                                    tvFilter.setText(text);
                                    tvFilter.setTag(position);
                                    refreshLayout.autoRefresh();
                                }
                            })
                            .show();
                }
            });
        }

        setTitleStr(type.getTag_name());

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    ivDelete.setVisibility(View.INVISIBLE);

                } else {
                    ivDelete.setVisibility(View.VISIBLE);
                }
            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager != null) {
                        inputMethodManager.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
                    }

                    if (TextUtils.isEmpty(etSearch.getText())) {
                        Toast.makeText(mContext, "请输入关键字", Toast.LENGTH_SHORT).show();
                    } else {
                        refreshLayout.autoRefresh();
                    }
                }
                return false;
            }
        });

        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
                page = 1;
                getUserInfoList();
            }
        });

        adapter = new IndexUserAdapter(mContext);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getUserInfoList();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getUserInfoList();
            }
        });
        refreshLayout.autoRefresh();
    }

    private void getUserInfoList() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page_index", page);
        params.put("page_size", size);
        params.put("tag_type", type.getType());
        if (!TextUtils.isEmpty(etSearch.getText())) {
            params.put("keyword", etSearch.getText().toString());
        }
        switch (type.getType()) {
            case 5: // 下级医养管家
                requestManager.request(requestManager.mJavaRetrofitService.getSubordinateList(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<ArrayList<Subordinate>>(refreshLayout) {
                            @Override
                            public void onSuccess(ArrayList<Subordinate> subordinates) {
                                super.onSuccess(subordinates);
                                setUserInfoList(subordinates);
                            }
                        });
                break;
            case 6: // 商家列表
                requestManager.request(requestManager.mJavaRetrofitService.getShopList(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<ArrayList<Shop>>(refreshLayout) {
                            @Override
                            public void onSuccess(ArrayList<Shop> shops) {
                                super.onSuccess(shops);
                                setUserInfoList(shops);
                            }
                        });
                break;
            case 4: // 健康异常用户端
            case 7: // 用户列表
                params.put("sort", tvFilter.getTag());
                requestManager.request(requestManager.mJavaRetrofitService.getCustomerList(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<ArrayList<Customer>>(refreshLayout) {
                            @Override
                            public void onSuccess(ArrayList<Customer> customers) {
                                super.onSuccess(customers);
                                setUserInfoList(customers);
                            }
                        });

                break;
        }
    }

    private void setUserInfoList(ArrayList<? extends UserInfo> userInfoList) {

        refreshLayout.setEnableLoadMore(userInfoList.size() >= size);
        if (page == 1) {
            if (userInfoList.size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
            adapter.refresh(userInfoList);
        } else {
            adapter.loadMore(userInfoList);
        }
    }
}
























